package com.yqg.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.DeleteJoinWrapper;
import com.yqg.manage.common.interceptor.SysLog;
import com.yqg.manage.controller.dto.RoleAndUnitDTO;
import com.yqg.manage.entity.RoleMenu;
import com.yqg.manage.entity.Unit;
import com.yqg.manage.entity.User;
import com.yqg.manage.mapper.RoleMapper;
import com.yqg.manage.service.IRoleMenuService;
import com.yqg.manage.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.IRoleService;
import com.yqg.manage.entity.Role;

import org.springframework.web.bind.annotation.RestController;

/**
 * RoleController 前端控制器
 *
 * @author suzhuang
 * @since 2023-07-05
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private IRoleService roleService;
    @Autowired
    private IRoleMenuService roleMenuService;
    @Resource
    private IUserService userService;

    @Resource
    private RoleMapper roleMapper;

    /**
     *  新增或者更新
     */
//    @PostMapping("/edit")
//    @Transactional(rollbackFor = Exception.class)
//    public Result save(@RequestBody Role role) {
//        if (role.getId() == null) {
//            role.setIsDelete("0");
//            roleService.saveOrUpdate(role);
//            ArrayList<RoleMenu> roleMenus = new ArrayList<>();
//            List<Long> menuIds = role.getMenuIds();
//            for (Long menuId : menuIds) {
//                RoleMenu roleMenu = new RoleMenu();
//                roleMenu.setMenuId(menuId);
//                roleMenu.setRoleId(role.getId());
//                roleMenus.add(roleMenu);
//            }
//            return Result.success(roleMenuService.saveBatch(roleMenus));
//        } else {
//            role.getMenuIds().add(role.getId());
//            roleService.saveOrUpdate(role);
//            return Result.success(roleMenuService.updateRoleMenu(role.getMenuIds()));
//        }
//    }

    /**
     * 删除时先进行判断
     */
    @GetMapping("delete/{id}")
    @SysLog("删除角色")
    public Result delete(@PathVariable Long id) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUserRole, id);
        List<User> list = userService.list(userLambdaQueryWrapper);
        if (list.size() > 0) {
            return Result.error("500", "当前角色下存在用户，禁止删除");
        }
        Role role = roleService.getById(id);
        role.setIsDelete("1");
        return Result.success(roleService.saveOrUpdate(role));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getIsDelete, "0")
                .ne(Role::getId, 1);
        List<Role> list = roleService.list(queryWrapper);
        return Result.success(list);
    }

    @GetMapping("/findAllRoleNoOne")
    public Result findAllRoleNoOne(@RequestParam Long roleLevel) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getIsDelete, "0")
                .eq(Role::getRoleNo, 1)
                .ne(Role::getId, 1)
                // 等级数字大的
                .gt(Role::getRoleLevel, roleLevel);
        List<Role> list = roleService.list(queryWrapper);
        return Result.success(list);
    }

    @GetMapping("/findAllRoleNoTwo")
    public Result findAllRoleNoTwo(@RequestParam Long roleLevel) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getIsDelete, "0")
                .eq(Role::getRoleNo, 2)
                // 等级数字大的
                .gt(Role::getRoleLevel, roleLevel);
        List<Role> list = roleService.list(queryWrapper);
        return Result.success(list);
    }


    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Long id) {
        return Result.success(roleService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Long pageNum,
                           @RequestParam Long pageSize,
                           @RequestParam(required = false) Long unitId,
                           @RequestParam Long roleLevel) {
        pageNum = (pageNum - 1) * pageSize;
        HashMap<String, Object> map = new HashMap<>();
        List<RoleAndUnitDTO> list = roleMapper.getRoleAndUnitData(pageNum, pageSize, unitId, roleLevel);
        Integer count = roleMapper.getRoleAndUnitDataCount(unitId, roleLevel);
        map.put("records", list);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 副书记获取的单位
     */
    @GetMapping("/deputyDirectorFindPage")
    public Result deputyDirectorFindPage(@RequestParam Long pageNum,
                                         @RequestParam Long pageSize,
                                         @RequestParam(required = false) Long unitId,
                                         @RequestParam Long roleLevel,
                                         @RequestParam Long userId) {
        pageNum = (pageNum - 1) * pageSize;
        HashMap<String, Object> map = new HashMap<>();
        List<RoleAndUnitDTO> list = roleMapper.deputyDirectorFindPage(pageNum, pageSize, unitId, roleLevel,userId);
        Integer count = roleMapper.deputyDirectorFindPageCount(unitId, roleLevel,userId);
        map.put("records", list);
        map.put("total", count);
        return Result.success(map);
    }

}

